#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time       :   18:22
# Author     : Y4
# Site       : 
# File       : 003 分组.py
# Software   : PyCharm


import re

# 匹配数字 0-100的数组

n = '100'
result = re.match(r'[1-9]?\d?$|100$',n)
print(result)                                  # <_sre.SRE_Match object; span=(0, 3), match='100'>


# 验证输入的邮箱  163   126     qq
email = '242886221@163.com'
result = re.match(r'\w{5,20}@(163|126|qq)\.(com|cn)$',email)
print(result)                                   # <_sre.SRE_Match object; span=(0, 17), match='242886221@163.com'>



# 不是以4或者7结尾的手机号码
phone = '18036311235'
result = re.match(r'1\d{9}[1235689]$',phone)
print(result)                                   # <_sre.SRE_Match object; span=(0, 11), match='18036311235'>



phone = '010-123456789'
result = re.match(r'(\d{3}|\d{4})-(\d{9})$',phone)
print(result.group())                           # 010-123456789
print(result.group(1))                          # 010
print(result.group(2))                          # 123456789


# 问题 : 标签不对应
msg = '<html>abc</h1>'
result = re.match(r'<\w+>(.+)</\w+>$',msg)
print(result)                                   # <_sre.SRE_Match object; span=(0, 16), match='<html>abc</html>'>
print(result.group(1))                          # abc


# 解决 : 对应标签  使用数字来达到前后匹配   \1 代表引用第一个()中的内容
msg = '<html>abc</html>'
result = re.match(r'<(\w+)>(.+)</\1>$',msg)
print(result)                                   # <_sre.SRE_Match object; span=(0, 16), match='<html>abc</html>'>
print(result.group(2))                          # abc


# 解决 : 对应标签  起名来达到前后匹配      (?P<name1>正则) 起名            (?P=name1) 引用
msg = '<html><h1>abc</h1></html>'
result = re.match(r'<(?P<name1>\w+)><(?P<name2>\w+)>(.+)</(?P=name2)></(?P=name1)>$',msg)
print(result)                                   # <_sre.SRE_Match object; span=(0, 25), match='<html><h1>abc</h1></html>'>
print(result.group(3))                          # abc



# 替换 (可函数)
def func(temp):
    num = temp.group()
    num1 = int(num) + 1
    return str(num1)


result = re.sub(r'\d+','你好啊','java100,python100')
print(result)                                   # java你好啊,python你好啊


result = re.sub(r'\d+',func,'java90,python100')
print(result)                                   # java91,python101




# 切割
result = re.split(r'[:,]','java100,python100')
print(result)                                   # ['java100', 'python100']


